package com.waimai.db.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.waimai.db.dao.BaseDao;
import com.waimai.db.dao.OrderDao;
import com.waimai.db.entity.Comment;
import com.waimai.db.entity.Order;
import com.waimai.db.entity.OrderFood;
import com.waimai.db.entity.OrderStatus;

@Repository
public class OrderDaoImpl extends BaseDao implements OrderDao {

	public OrderFood addOrderFood(OrderFood orderFood) {
		getSession().save(orderFood);
		return orderFood;
	}

	public Order findById(Integer orderId) {
		return (Order) getSession().get(Order.class, orderId);
	}

	@SuppressWarnings("unchecked")
	public List<Order> getUserOrders(Integer userId) {
		String hql = "FROM Order o WHERE o.user.userId=:userId ORDER BY o.orderId DESC";
		return getSession().createQuery(hql).setParameter("userId", userId)
				.list();
	}

	public OrderStatus updateStatus(Integer orderId, OrderStatus orderStatus) {
		Order order = findById(orderId);
		order.setOrderstatus(orderStatus);
		return orderStatus;
	}

	public Comment addComment(Integer orderId, Comment comment) {
		getSession().save(comment);
		Order order = findById(orderId);
		order.setComment(comment);
		return comment;
	}

	public int count() {
		// TODO Auto-generated method stub
		String hql = "from Order o";
		return getSession().createQuery(hql).list().size();
	}

	public List<Order> getOrders() {
		// TODO Auto-generated method stub
		String hql = "";
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Order> getOrdersByAdminId(int adminId) {
		// TODO Auto-generated method stub
		String hql = "select r.restaurantName,r.phone,r.address,o.orderTime,o.remark,s.statusName from Restaurant r,Order o,OrderStatus s where r.restaurantId=o.restaurant.restaurantId and o.orderstatus.statusId=s.statusId and r.admin.adminId=?";
		return getSession().createQuery(hql).setParameter(0, adminId).list();
	}
}
